package shiro.sso.chapter01.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.*;

/**
 * Create by cheng on 2019/5/17 23:25
 */
@Controller
public class LoginController {

    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            return "/index";
        }
        return "/login";
    }


    @PostMapping("/login")
    public String login(String username,String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        subject.login(token);
        return "/index";
    }

    @RequestMapping("/logout")
    @ResponseBody
    public String logout(HttpServletRequest request, HttpSession session) {
        SecurityUtils.getSubject().logout();
        //这个是直接退出，走的是默认退出方式
//        return "redirect:http://127.0.0.1:8080/cas/logout";
        return "退出";
    }

    @RequestMapping("/test1")
    @ResponseBody
    public String test1() {
        return "成功";
    }

    @RequestMapping("/test2")
    @ResponseBody
    public String test2() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            return "已登录";
        } else {
            return "未登陆";
        }

    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
        System.out.println(session.getId());
        System.out.println(SecurityUtils.getSubject().getSession().getId());
        //获取session的Id
        String sessionId = session.getId();
        //将session的Id存储到名字为JSESSIONID的cookie中
        Cookie cookie = new Cookie("JSESSIONID", sessionId);
        //设置cookie的有效路径
        cookie.setPath(request.getContextPath());
        response.addCookie(cookie);
        return "test";
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpSession session) {
        System.out.println(session.getId());
        System.out.println(SecurityUtils.getSubject().getSession().getId());
        return "/index";
    }



    //    @RequestMapping("/login")
//    public String login(HttpServletRequest request) {
//        String exception = (String) request.getAttribute("shiroLoginFailure");
//        String msg;
//        System.out.println("exception=" + exception);
//                if (exception != null) {
//            if (UnknownAccountException.class.getName().equals(exception)) {
//                System.out.println("UnknownAccountException -- > 账号不存在：");
//                msg = "UnknownAccountException -- > 账号不存在：";
//            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
//                System.out.println("IncorrectCredentialsException -- > 密码不正确：");
//                msg = "IncorrectCredentialsException -- > 密码不正确：";
//            } else if ("kaptchaValidateFailed".equals(exception)) {
//                System.out.println("kaptchaValidateFailed -- > 验证码错误");
//                msg = "kaptchaValidateFailed -- > 验证码错误";
//            } else {
//                msg = "else >> "+exception;
//                System.out.println("else -- >" + exception);
//            }
//        }
//
////        Subject currentUser = SecurityUtils.getSubject();
////        UsernamePasswordToken token = new UsernamePasswordToken("admin","admin");
////        currentUser.login(token);
//        return "/login";
//    }

}
